package com.example.ffepeg.ffmTestIng;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class FFmpegLogoReplacer {

    //为视频添加logo
    public static void main(String[] args) {
        String inputVideoPath = "https://cdn1.suno.ai/2eb0cec3-cfcb-4a57-b219-381fcc44c4f1.mp4";
        String outputPath = "D:\\javaXM\\FFEpeg\\src\\main\\resources\\out\\outputWithMusic.mp4";
        //String newLogoPath = "C:\\Users\\Administrator\\Desktop\\img.png";  //logo
        String logoImagePath = "https://pushimg.liecaizhijia.com/image_example/img/c6dd68e8-31da-4af8-b255-14082a279fff51826714613360d6ca65957966e675a.png";
        int x = 300;
        int y = 700;


        // 构建FFmpeg命令
        String[] command = {
                "ffmpeg",
                "-i", inputVideoPath,
                "-i", logoImagePath,
                "-filter_complex", "[0:v][1:v]overlay=300:700",
                "-c:a", "copy",
                outputPath
        };
        // 创建ProcessBuilder并设置命令
        ProcessBuilder processBuilder = new ProcessBuilder(command);

        try {
            // 启动进程
            Process process = processBuilder.start();

            // 读取标准输出
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);  // 打印FFmpeg的输出，这里可以做进一步的解析
            }

            // 等待进程完成并检查退出码
            int exitCode = process.waitFor();
            if (exitCode == 0) {
                System.out.println("Logo replacement successful.");
            } else {
                System.err.println("Error occurred while replacing the logo. Exit code: " + exitCode);
            }
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }

    }
}

